// pagesDoctor/adviceInfoByID/index.js
import {
  ApiAdviceInfoByID,
  ApiAdviceRollback
} from '../../api/docIndex'
const app = getApp()
Page({

  
  /**
   * 页面的初始数据
   */
  data: {
    /*   TobuyMedicine */
  },

  submitBtn() {
   /*  medicine=[{"id":604,"count":"2","price":"46.60","name":"★平消片(基)"}]&sum=93.2&medicId=104
 */

let sum =0
let medicine=[]
this.data.adviceArr.medical.forEach(item=>{
  medicine.push({id:item.id,count:item.num,price:item.price,name:item.name,DrugMinUnit:item.DrugMinUnit})
  sum +=(Number(item.price)*Number(item.num))
})
 let query= {
  medicId:this.data.adviceArr.id,
  sum,
  medicine:JSON.stringify(medicine)
}
wx.navigateTo({
  url: `/pages/payment/index?medicine=${query.medicine}&&sum=${query.sum}&&medicId=${query.medicId}`,
})
/*   console.log(query) */
   /*   wx.navigateTo({
      url: `/pages/TobuyMedicine/index?data=${JSON.stringify(this.data.adviceArr.medical)}&&id=${this.data.adviceArr.id}`,
    })   */
  },

  handleAdvice(e) {
    let file_path = e.currentTarget.dataset.v
    //查看电子处方
    wx.showLoading({
      title: '加载中...',
      success: () => {
        wx.downloadFile({
          url: file_path,
          success: (res) => {
            const filePath = res.tempFilePath
            wx.hideLoading()
            this.openView(filePath)
          },
          fail: () => {
            wx.showToast({
              title: '文件下载失败！',
              icon: "error"
            })
          }
        })
      }
    })
  },
  openView(path) {
    wx.openDocument({
      filePath: path,
      success: function (res) {

      },
      fail: () => {
        wx.showToast({
          title: '文件打开失败！',
          icon: "error"
        })
      }
    })
  },

  //重新开方
  newPrescription() {
    let data = {
      order_id: this.data.adviceArr.order_id,
      uid: this.data.adviceArr.uid,
      patient_id: this.data.adviceArr.patient_id,
      ICD: this.data.adviceArr.ICD,
      ICD1: this.data.adviceArr.ICD1,
      type: this.data.adviceArr.type,
      disease: this.data.adviceArr.disease,
      main: this.data.adviceArr.main,
      medical: this.data.adviceArr.medical,
    }
    wx.redirectTo({
      url: `/pagesDoctor/prescribing/index?adviceArr=${JSON.stringify(data)}`,
    })
  },

  //撤回处方
  withdrawaPrescription() {
    let that = this
    wx.showModal({
      title: "是否确认撤回该处方？",
      success(res) {
        if (res.confirm) {
          ApiAdviceRollback(
            that.data.adviceArr.id
          ).then(res => {
            if (res.code == '200') {
              wx.showToast({
                title: "提交成功",
              })
              setTimeout(() => {
                wx.navigateBack()
              }, 1000);
            }
          })
        }

      }
    })

  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      id: options.id
    })
  },

  //获取处方明细单
  getPreDetail(id) {
    wx.showLoading({
      title: '加载中'
    })
    ApiAdviceInfoByID(id).then(res => {
      res.data.medical = JSON.parse(res.data.medical)
      this.setData({
        adviceArr: res.data
      })
      wx.hideLoading()
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    app.setWsIsClose(true)
    this.getPreDetail(this.data.id)
    this.setData({
      loginType: wx.getStorageSync('loginType')
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})